package com.songqintuan.mall.controller;

import java.io.IOException;



import javax.faces.FactoryFinder;
import javax.faces.application.Application;
import javax.faces.application.NavigationHandler;
import javax.faces.context.FacesContext;
import javax.faces.context.FacesContextFactory;
import javax.faces.lifecycle.Lifecycle;
import javax.faces.lifecycle.LifecycleFactory;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.mall.constant.MallConstants;


/**
 * Servlet Filter implementation class LoggedInCheckFilter
 */
@SuppressWarnings("serial")
public class LoggedInCheckFilter extends HttpServlet implements Filter {

	protected FilterConfig filterConfig;
	private static final String CONTENT_TYPE = "text/html; charset=UTF-8";
    /**
     * Default constructor. 
     */
    public LoggedInCheckFilter() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
		this.filterConfig = null; 
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		// place your code here

		// pass the request along the filter chain
		FacesContext fc = SpringFacesUtil.getFacesContext();
		if (null == fc) {
			FacesContextFactory contextFactory = (FacesContextFactory)FactoryFinder.getFactory(FactoryFinder.FACES_CONTEXT_FACTORY);
			  LifecycleFactory lifecycleFactory = (LifecycleFactory)FactoryFinder.getFactory(FactoryFinder.LIFECYCLE_FACTORY);
			  Lifecycle lifecycle = lifecycleFactory.getLifecycle(LifecycleFactory.DEFAULT_LIFECYCLE);

			  // Either set a private member servletContext = filterConfig.getServletContext();
			  // in you filter init() method or set it here like this:
			  ServletContext servletContext = ((HttpServletRequest)request).getSession().getServletContext();
			  // Note that the above line would fail if you are using any other protocol than http

			  // Doesn't set this instance as the current instance of FacesContext.getCurrentInstance
			  fc = contextFactory.getFacesContext(servletContext, request, response, lifecycle);
		}
		Application application = fc.getApplication(); 
		HttpServletRequest req = (HttpServletRequest) request;   
		HttpServletResponse res = (HttpServletResponse) response;   
		HttpSession session = req.getSession();
		res.setContentType(CONTENT_TYPE);
		//res.setCharacterEncoding("UTF-8");
		if (!loggedIn()) {
			SpringFacesUtil.redirectPage(MallConstants.GO_TO_LOGIN_XHMTL);
			
		} else {
			chain.doFilter(request, response);
		}
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		this.filterConfig = fConfig; 
		// TODO Auto-generated method stub
	}

	 //验证是否已经登陆
	 private boolean loggedIn() {
		 return LoginController.loggedIn();
	 }
}
